/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.permission.controller;


import java.util.ArrayList;
import java.util.List;
import queenb.net.entitybean.N900;
import queenb.net.entitybean.Q200;
import queenb.web.controller.QueenbController;
import queenb.web.crm.store.N900Store;
import queenb.web.permission.store.Q200Store;
import queenb.web.util.Util;

/**
 *
 * @author VANANH
 */
public class Q200Controller extends QueenbController{
    private int pq350;
    private int pn850;
    private int fq100;
    private int fn100;
    private Q200Store q200Store;
    private int actionReturnCode;
    
    public Q200Controller()
    {
       Object pq350Object = getRequest().getParameter("pq350") ;
       if(pq350Object != null)
       {
           pq350 = Util.parseInt(pq350Object.toString());
       }
    }
    
    public List<Q200> getListQ200L()
    {
        try{
            q200Store = new Q200Store();
            List<Q200> list200 = q200Store.getListQ200L(pq350);
            
            return list200;
        }catch(Exception ex)
        {
            return null;
        }
        
    }
    
    public List<Q200> getListQ200R()
    {
        try{
            q200Store = new Q200Store();
            List<Q200> list200R = q200Store.getListQ200R(pq350);
            
            return list200R;
        }catch(Exception ex)
        {
            return null;
        }
        
    }
    
    public int getActionQ200()
    {
        Object listIdQ200 = getRequest().getParameter("idemploy");
         Object pq350Object = getRequest().getParameter("pq350");
         if(listIdQ200 != null && pq350Object != null && !listIdQ200.toString().equals(""))
         {
             ArrayList<Integer> listIdN900Arr = new ArrayList<Integer>();
             String[] listIdN900String = listIdQ200.toString().split("_");
             for(int i = 0 ; i < listIdN900String.length; i++)
             {
                 listIdN900Arr.add(Util.parseInt(listIdN900String[i].toString()));
             }
             if(listIdN900Arr.size() > 0)
             {
                 saveQ200ToGroup(listIdN900Arr, Util.parseInt(pq350Object.toString()));
             }
         }
         if(listIdQ200.toString().equals(""))
         {
             ArrayList<Integer> listIdN900Arr = new ArrayList<Integer>();
             
             if(listIdN900Arr.size() == 0)
             {
                 deleteAllN900(listIdN900Arr, Util.parseInt(pq350Object.toString()));
             }
            
         }
        return actionReturnCode;
        
    }
    
     public void saveQ200ToGroup(ArrayList<Integer> listIdEmployee, int groupEmployee) {
        //Chon ra nhung id nao ma khong co trong list id dc submit len de xoa:
        deleteEmployeeFromGroup(listIdEmployee, groupEmployee);
        //Chon ra nhung id trong list id dc submit len, ma khong co trong db thi tien hanh insert
        insertEmployee(listIdEmployee, groupEmployee);

    }
     
     public void deleteEmployeeFromGroup(ArrayList<Integer> listIdEmployee, int groupEmployee) {
        System.out.println("$$$$$$$$$$$$$$$$$$$groupEmployee ne: " + groupEmployee);
        List<Q200> listEmployeeInGroup = null;
        q200Store = new Q200Store();
        try {
            listEmployeeInGroup = q200Store.getListQ200R(groupEmployee);
             System.out.println("================?>>>" + listEmployeeInGroup.size());
        } catch (Exception ex) {
            System.out.println("Error when get employee in group. ERROR CODE: " + ex.getMessage());
        }
        if (listEmployeeInGroup != null) {
            for (int i = 0; i < listEmployeeInGroup.size(); i++) {
                if (!listIdEmployee.contains(listEmployeeInGroup.get(i).getFn100())) {
                    try {
                       // getN900Store().deleteN900(listEmployeeInGroup.get(i).getPn100(), groupEmployee, getLogin().getUserName());
                            q200Store.deleteQ200(7, groupEmployee,listEmployeeInGroup.get(i).getFn100(), getLogin().getUserName());
                    } catch (Exception ex) {
                       System.out.println("Error when delete employee in group. ERROR CODE: " + ex.getMessage());
                    }
                }
            }
        }
    }

    //Xoa het tat cac cac nhan vien thuoc nhom
    public void deleteAllN900(ArrayList<Integer> listIdEmployee,int groupEmployee) {
         List<Q200> listEmployeeInGroup = null;
         q200Store = new Q200Store();
         try {
            listEmployeeInGroup = q200Store.getListQ200R(groupEmployee);
             System.out.println("================?>>>" + listEmployeeInGroup.size());
        } catch (Exception ex) {
            System.out.println("Error when get employee in group. ERROR CODE: " + ex.getMessage());
        }
        if (listEmployeeInGroup != null) {
            for (int i = 0; i < listEmployeeInGroup.size(); i++) {
                if (!listIdEmployee.contains(listEmployeeInGroup.get(i).getFn100())) {
                    try {
                       // getN900Store().deleteN900(listEmployeeInGroup.get(i).getPn100(), groupEmployee, getLogin().getUserName());
                            q200Store.deleteQ200(7, groupEmployee,listEmployeeInGroup.get(i).getFn100(), getLogin().getUserName());
                    } catch (Exception ex) {
                       System.out.println("Error when delete employee in group. ERROR CODE: " + ex.getMessage());
                    }
                }
            }
        }
//        try {
//            System.out.println("delete all with group: "+groupEmployee);
//            q200Store.deleteQ200(7, pq350, fn100 ,  getLogin().getUserName()) ;
//        } catch (Exception ex) {
//            System.out.println("Error when delete All N900. ERROR CODE: " + ex.getMessage());
//        }

    }

    public void insertEmployee(ArrayList<Integer> listIdEmployee, int groupEmployee) {        
        List<Q200> listEmployeeInGroup = null;
        q200Store = new Q200Store();
        try {
            listEmployeeInGroup = q200Store.getListQ200R(groupEmployee);
            System.out.println("================?>>>" + listEmployeeInGroup.size());
        } catch (Exception ex) {
            System.out.println("Error when get employee in group. ERROR CODE: " + ex.getMessage());
        }
        //Truong hop trong database chua co nhan vien nao thuoc nhom
        if (listEmployeeInGroup == null) {
            for (int i = 0; i < listIdEmployee.size(); i++) {
                try {
                   // getN900Store().updateN900(listIdEmployee.get(i), groupEmployee, null, null, null, null, getLogin().getUserName());
                    q200Store.updateQ200(7, pq350, listIdEmployee.get(i), getLogin().getUserName()) ;
                } catch (Exception ex) {
                    System.out.println("Error when insert employee with Id= " + listIdEmployee.get(i) + " and groupEmployee="
                            + groupEmployee + ". ERROR CODE: " + ex.getMessage());
                }
            }
        } //Truong hop chon nhung id trong list id dc submit len de save vao
        else {
            ArrayList<Integer> listIdInGroup = new ArrayList<Integer>();
            for (int i = 0; i < listEmployeeInGroup.size(); i++) {
                listIdInGroup.add(listEmployeeInGroup.get(i).getFn100());
            }
            for (int i = 0; i < listIdEmployee.size(); i++) {
                if (!listIdInGroup.contains(listIdEmployee.get(i))) {
                    try {
                       // getN900Store().updateN900(listIdEmployee.get(i), groupEmployee, null, null, null, null, getLogin().getUserName());
                        q200Store.updateQ200(7, pq350, listIdEmployee.get(i), getLogin().getUserName()) ;
//                        System.out.println("Insert moi vo: "+listIdEmployee.get(i));
                    } catch (Exception ex) {
                         System.out.println("Error when insert employee with Id= " + listIdEmployee.get(i) + " and groupEmployee="
                                + groupEmployee + ". ERROR CODE: " + ex.getMessage());
                    }
                }
            }
        }
    }



    /**
     * @return the pq350
     */
    public int getPq350() {
        return pq350;
    }

    /**
     * @param pq350 the pq350 to set
     */
    public void setPq350(int pq350) {
        this.pq350 = pq350;
    }

    /**
     * @return the pn850
     */
    public int getPn850() {
        return pn850;
    }

    /**
     * @param pn850 the pn850 to set
     */
    public void setPn850(int pn850) {
        this.pn850 = pn850;
    }

    /**
     * @return the fq100
     */
    public int getFq100() {
        return fq100;
    }

    /**
     * @param fq100 the fq100 to set
     */
    public void setFq100(int fq100) {
        this.fq100 = fq100;
    }

    /**
     * @return the fn100
     */
    public int getFn100() {
        return fn100;
    }

    /**
     * @param fn100 the fn100 to set
     */
    public void setFn100(int fn100) {
        this.fn100 = fn100;
    }
}
